define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'service/shop/index' + location.search,
                    add_url: 'service/shop/add',
                    edit_url: 'service/shop/edit',
                    multi_url: 'service/shop/multi',
                    import_url: 'service/shop/import',
                    table: 'service_shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: '店铺ID',operate: false},
                        {field: 'cover_img', title: '封面图', formatter: Table.api.formatter.image, events: Table.api.events.image,operate: false},
                        {field: 'name', title:'店铺名称', operate: 'LIKE'},
                        {field: 'service.name', title: __('Type'),formatter: Table.api.formatter.flag,sortable: true,operate: false},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'main_business', title: __('Main_business'), operate: false},
                        {field: 'province.name', title: __('Province'), operate: false},
                        {field: 'city.name', title: __('City'),operate: false},
                        {field: 'districts.name', title: __('Districts'),operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE',
                            cellStyle : function(value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        // "text-overflow": "ellipsis",
                                        "overflow": "auto",
                                        "max-width": "300px"
                                    }
                                };}
                        },

                        {field: 'type', title: '类型',searchList: {"1":'个人','2':'商家'}, formatter: Table.api.formatter.status},
                        {field: 'shop.shop_name', title: '商家名称', operate: 'LIKE',
                            cellStyle : function(value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        // "text-overflow": "ellipsis",
                                        "overflow": "auto",
                                        "max-width": "250px"
                                    }
                                };}
                        },
                        {field: 'member.member_name', title: '发布者',operate: 'LIKE'},
                        {field: 'show', title: '是否上架（发布者）',searchList: {"1":'上架','2':'下架'}, formatter: Table.api.formatter.status},
                        {field: 'status', title: '平台审核',searchList: {"0":'待审核',"1":'审核通过','2':'关闭'}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '详情',
                                    title: '详情',
                                    classname: 'btn btn-xs btn-info btn-editone'
                                },
                                {
                                    name: '通过',
                                    text: '通过',
                                    title: '通过',
                                    confirm: '确认审核通过吗？',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'service/shop/pass',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 0 || row.status == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }

                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                },
                                {
                                    name: '关闭',
                                    text: '关闭',
                                    title: '关闭',
                                    confirm: '确认关闭吗？',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    url: 'service/shop/down',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 1 || row.status == 0) {
                                            return true;
                                        } else {
                                            return false;
                                        }

                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
